const mongoose = require('./model/db');
const { User } = require('./model/model');

async function addAdminUser() {
  try {
    console.log('🔍 检查admin用户...');
    
    // 检查是否已存在admin用户
    const existingAdmin = await User.findOne({ username: 'admin' });
    
    if (existingAdmin) {
      console.log('✅ admin用户已存在');
      console.log('用户信息:', {
        id: existingAdmin.id,
        username: existingAdmin.username,
        role: existingAdmin.role
      });
    } else {
      console.log('➕ 创建admin用户...');
      const adminUser = new User({
        id: '100001',
        username: 'admin',
        password: '123456'
      });
      await adminUser.save();
      console.log('✅ admin用户创建成功');
      console.log('用户信息:', {
        id: adminUser.id,
        username: adminUser.username,
        role: adminUser.role
      });
    }
    
    console.log('\n🎉 完成！');
    console.log('现在可以使用以下账号登录:');
    console.log('- 用户名: admin');
    console.log('- 密码: 123456');
    
    process.exit(0);
  } catch (error) {
    console.error('❌ 添加admin用户失败:', error);
    process.exit(1);
  }
}

addAdminUser(); 